package com.atguigu.servicearticle.controlle;

import com.atguigu.commonutils.R;
import com.atguigu.servicearticle.entity.EduArticl;
import com.atguigu.servicearticle.service.impl.EduArticlService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cyt
 * @create 2020-06-23 下午 4:40
 */

@RequestMapping("/servicearticle")
@RestController
public class articleController {

    @Autowired
    EduArticlService eduArticlService;

    // 分页查询
    @GetMapping("/pageQuery/{size}/{page}")
    @ResponseBody
    public R pageQuery(@PathVariable("size") Long size, @PathVariable("page") Long page) {
        Page<EduArticl> articlPage = new Page<>(page, size);
        // 分页查询
        eduArticlService.selectPage(articlPage);
        List<EduArticl> records = articlPage.getRecords();
        long total = articlPage.getTotal();
        return R.ok().data("list", records).data("total", records.size()).data("data", articlPage);
    }


    // 更新
    @ResponseBody
    @PostMapping("/update/{id}")
    public R update(@RequestBody EduArticl eduArticl) {
        eduArticlService.updateById(eduArticl);
        return R.ok();
    }


    // 删除
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id) {
        eduArticlService.removeById(id);
        return R.ok();
    }

    /**
     * 查询出一个信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/getInfo/{id}")
    public R getInfo(@PathVariable("id") String id) {
        QueryWrapper<EduArticl> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        EduArticl eduArticl = eduArticlService.getOne(wrapper);
        return R.ok().data("article", eduArticl);
    }

    /**
     * 保存文章
     *
     * @param eduArticl
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    public R save(@RequestBody EduArticl eduArticl) {
        eduArticlService.save(eduArticl);
        return R.ok();
    }

}
